package com.zlq.cn.service.ai;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.volcengine.error.SdkError;
import com.volcengine.helper.Const;
import com.volcengine.model.ApiInfo;
import com.volcengine.model.response.RawResponse;
import com.volcengine.service.visual.IVisualService;
import com.volcengine.service.visual.VisualConfig;
import com.volcengine.service.visual.impl.VisualServiceImpl;
import com.zlq.cn.config.FinderConfiguration;
import com.zlq.cn.dto.visual.request.AllAgeGenerationRequest;
import com.zlq.cn.dto.visual.request.DollyZoomRequest;
import com.zlq.cn.dto.visual.response.AllAgeGenerationResponse;
import com.zlq.cn.dto.visual.response.DollyZoomResponse;
import one.util.streamex.StreamEx;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@Service
public class MyVisualService {
    private static final String VERSION_1 = "2020-08-26";
    private static final String VERSION_2 = "2022-08-31";
    private static final String DollyZoom = "DollyZoom";
    private static final String AllAgeGeneration = "AllAgeGeneration";
    public final IVisualService visualService;

    public MyVisualService(FinderConfiguration configuration) {
        visualService = VisualServiceImpl.getInstance();
        visualService.setAccessKey(configuration.getVisualSecretId());
        visualService.setSecretKey(configuration.getVisualSecretKey());
        putApiInfo(DollyZoom, VERSION_1);
        putApiInfo(AllAgeGeneration, VERSION_2);
    }

    public DollyZoomResponse dollyZoom(DollyZoomRequest request) throws Exception {
        RawResponse response = visualService.post(DollyZoom, null, convertNameValuePair(request));
        if (response.getCode() != SdkError.SUCCESS.getNumber()) {
            throw response.getException();
        }
        return JSON.parseObject(new String(response.getData(), StandardCharsets.UTF_8), DollyZoomResponse.class);
    }

    public AllAgeGenerationResponse allAgeGeneration(AllAgeGenerationRequest request) throws Exception {
        request.setReqKey("all_age_generation");
        RawResponse response = visualService.json(AllAgeGeneration, null,JSON.toJSONString(request));
        if (response.getCode() != SdkError.SUCCESS.getNumber()) {
            throw response.getException();
        }
        return JSON.parseObject(new String(response.getData(), StandardCharsets.UTF_8), AllAgeGenerationResponse.class);
    }

    private List<NameValuePair> convertNameValuePair(Object obj) throws IllegalArgumentException {
        JSONObject jsonObject = (JSONObject) JSON.toJSON(obj);
        return StreamEx.of(jsonObject.entrySet())
                .filter(it -> Objects.nonNull(it.getValue()))
                .map(it -> (NameValuePair) new BasicNameValuePair(it.getKey(), it.getValue().toString()))
                .toList();
    }

    private void putApiInfo(String api, String version) {
        VisualConfig.apiInfoList.put(api, new ApiInfo(
                new HashMap<String, Object>() {
                    {
                        put(Const.Method, "POST");
                        put(Const.Path, "/");
                        put(Const.Query, new ArrayList<NameValuePair>() {
                            {
                                add(new BasicNameValuePair("Action", api));
                                add(new BasicNameValuePair("Version", version));
                            }
                        });
                    }
                }
        ));
    }
}
